﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using Microsoft.Xna.Framework.Content;
using GameLayer;
using System.Xml;
using GameInstrument;
using GameInterface;
using GameCharacter;

namespace MainGame.GameInstrument
{
    public class DragonBall : Button, ICollision<Weapon>, ICollision<Character>
    {
        private static bool initialized;
        private static Sprite normalSprite;
        private static Sprite superSprite;
        private DragonBallType _type;
        private bool _completePreparePower = true;

        public bool CompletePreparePower
        {
            get { return _completePreparePower; }
            set { _completePreparePower = value; }
        }
        public int Durability { get; set; }
        public DragonBallType Type
        {
            get { return _type; }
            set
            {
                _type = value;
                _index = (int)value;
            }
        }

        public enum DragonBallType : int
        {
            Normal = 0,
            super = 1
        }
        public override int Index
        {
            get
            {
                return base.Index;
            }
            protected set
            {
                base.Index = value;
                _type = (DragonBallType)value;
            }
        }
        public ContentManager Content { get; set; }
        public DragonBall(ContentManager content)
        {
            if (!initialized)
            {
                normalSprite = new Sprite(content, "Images/Sprites/DragonBall/Normal/DragonBall_", 3);
                superSprite = new Sprite(content, "Images/Sprites/DragonBall/Super/SuperDragonBall_", 3);
                initialized = true;
            }
            this.Items.AddRange(new[] { (Sprite)normalSprite.Clone(), (Sprite)superSprite.Clone() });
        }
        public DragonBall(ContentManager content, XmlElement xml)
            : this(content)
        {
            int x = int.Parse(xml.GetAttribute("x"));
            int y = int.Parse(xml.GetAttribute("y"));
            this.Position = new Microsoft.Xna.Framework.Vector2(x, y);
            this.Type = (DragonBallType)int.Parse(xml.GetAttribute("type"));
            Durability = 15;
        }
        public void SetType(DragonBallType dragonBallType)
        {
            this.Index = (int)dragonBallType;
        }
        protected override void UpdateEntitys(Microsoft.Xna.Framework.GameTime gameTime, object extraData)
        {
            base.UpdateEntitys(gameTime, extraData);
            Current.Update(gameTime);
            Current.NextFrame();
        }

        public List<Weapon> ReleasePowerAttack()
        {
            if (Durability < 0 && CompletePreparePower)
            {
                BigExplosive exposive = new BigExplosive(Content);
                exposive.Position = this.Position + this.Size / 2;
                exposive.Power = 10;
                CompletePreparePower = false;
                exposive.Explosive();
                return new List<Weapon>() { exposive };
            }
            return null;
        }

        public bool CollisionWith(Weapon other, bool pixelLevel)
        {
            return other.CollisionWith(Current,pixelLevel);
        }

        public bool CollisionWith(Character other, bool pixelLevel)
        {
            return other.CollisionWith(Current, pixelLevel);
        }
    }
}
